package solution;

/*
* 641
* */
public class MyCircularDeque {
    /*
    * 模仿Java类库使用链表实现
    * */

    /*
    * 使用数组实现
    * */
    static class Solution1 {
        int[] myqueue;
        int front; //队头指针
        int reer; //队尾指针
        int size; //队列当前的大小
        int capacity; //队列的容量


        /** Initialize your data structure here. Set the size of the deque to be k. */
        public Solution1(int k) {
            this.myqueue = new int[k];
            this.front = 0;
            this.reer = 0;
            this.size = 0;
            this.capacity = k;
        }

        /** Adds an item at the front of Deque. Return true if the operation is successful. */
        public boolean insertFront(int value) {
            if (reer == front && size == capacity){
                return false;
            }else {
                front = (front + capacity - 1) % capacity;
                myqueue[front] = value;
                size++;
                return true;
            }
        }

        /** Adds an item at the rear of Deque. Return true if the operation is successful. */
        public boolean insertLast(int value) {
            if (reer == front && size == capacity){
                return false;
            }else {
                myqueue[reer] = value;
                reer = (reer + capacity + 1) % capacity;
                size++;
                return true;
            }
        }

        /** Deletes an item from the front of Deque. Return true if the operation is successful. */
        public boolean deleteFront() {
            if (reer == front && size == 0){
                return false;
            }else {
                front = (front + 1) % capacity;
                size--;
                return true;
            }
        }

        /** Deletes an item from the rear of Deque. Return true if the operation is successful. */
        public boolean deleteLast() {
            if (reer == front && size == 0){
                return false;
            }else {
                reer = (reer + capacity - 1 ) % capacity;
                size--;
                return true;
            }
        }

        /** Get the front item from the deque. */
        public int getFront() {
            if (reer == front && size == 0){
                return -1;
            }else {
                int frontE = myqueue[front];
                return frontE;
            }
        }

        /** Get the last item from the deque. */
        public int getRear() {
            if (reer == front && size == 0){
                return -1;
            }else {
                return myqueue[(reer + capacity - 1) % capacity];
            }
        }

        /** Checks whether the circular deque is empty or not. */
        public boolean isEmpty() {
            return (reer == front) && size == 0;
        }

        /** Checks whether the circular deque is full or not. */
        public boolean isFull() {
            return reer == front && size == capacity;
        }
    }


}
